/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.nio.ByteBuffer;

/**
 *
 * @author Dunekacke
 */
public class Checksum {
final static int BYTELENGTH = 4;

    public static int calculate(Packet packet)
    {
        int seq = packet.sequence;
        int packetLength = packet.contentLength;
        byte[] content = packet.content;
        byte flag = packet.flag;
        
        
        String uniform = "";
        uniform = Integer.toString(packetLength);
        uniform += new String(content);
        uniform += Byte.toString(flag);
        uniform += Integer.toString(seq);
        while(uniform.length() > BYTELENGTH)
        {
            char x = uniform.charAt(0);
            char y = uniform.charAt(1);
            char z = uniform.charAt(2);
            char newChar = (char)((x + y) * z % 89 + 33);
            uniform = uniform.substring(2);
            uniform += newChar;
        }
        byte[] byteValue;
        byteValue = uniform.getBytes();
		ByteBuffer wrapped = ByteBuffer.wrap(byteValue);
		int returnValue = wrapped.getInt();
        return returnValue;
    }
}

/*
array 

while though file
	if !empty file
		array = bites in file
	else
		array = special case
	



*/
